package com.sky.service.impl;

import ch.qos.logback.core.status.StatusManager;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.Statement;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Properties;

@Service
@Slf4j
public class CategoryServicelmpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;



    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询:{}", categoryPageQueryDTO);
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setName(categoryPageQueryDTO.getName());
        categoryDTO.setType(categoryPageQueryDTO.getType());
        Page<Category> page = categoryMapper.pageQuery(categoryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.save(category);
    }

    @Override
    public void Delete(Long id) {
        categoryMapper.Delete(id);
    }

    @Override
    public List<Category> list(Integer type) {
        log.info("根据类型查询菜品:{}", type);
        // 如果 type 为 null，则查询所有分类
        if (type == null)
            return categoryMapper.alllist();
        return categoryMapper.list(type);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
         Category category = Category.builder().status(status).id(id).build();
         categoryMapper.update(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
         Category category = new Category();
         BeanUtils.copyProperties(categoryDTO, category);
         category.setUpdateTime(LocalDateTime.now());
         category.setUpdateUser(BaseContext.getCurrentId());
         categoryMapper.update(category);
    }
}
